local TweenService = game:GetService("TweenService")
local UIRoot=Instance.new("ScreenGui",game.Players.LocalPlayer:WaitForChild("PlayerGui"))
UIRoot.IgnoreGuiInset=false
UIRoot.ResetOnSpawn=false
local M={}
local world2ScreenTween = TweenInfo.new(0.5, Enum.EasingStyle.Linear);
local screen2WorldTween = TweenInfo.new(1, Enum.EasingStyle.Cubic);
local ui2uiExplodeTween = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.Out)
M.world2Screen = function(PullFuc,PushFuc,WorldPos,ScreenPos,endSize,startSize)

    if not startSize then
      startSize=UDim2.fromOffset(endSize.X/2,endSize.Y/2)
    else
      startSize=UDim2.fromOffset(startSize.X,startSize.Y)
    end
   
    endSize=UDim2.fromOffset(endSize.X,endSize.Y)
    
    ScreenPos=UDim2.fromOffset(ScreenPos.X,ScreenPos.Y)

   local image=PullFuc()
   image.Parent=UIRoot
   image.Visible=true
  local startPos=  game.Workspace.CurrentCamera:WorldToScreenPoint(WorldPos)
  image.Position=UDim2.fromOffset(startPos.X,startPos.Y)
  image.Size=startSize
  local t = TweenService:Create(image,world2ScreenTween, {
    Size=endSize,
    Position=ScreenPos
})
  t.Completed:Connect(function()
    t:Destroy()
    PushFuc(image)
  end)
  t:Play()

end;

M.screen2World = function(PullFuc,PushFuc,WorldPos,ScreenPosOrg,ScreenPos,endSize,startSize)

          if not startSize then
            startSize=UDim2.fromOffset(endSize.X/2,endSize.Y/2)
          else
            startSize=UDim2.fromOffset(startSize.X,startSize.Y)
          end
        
          endSize=UDim2.fromOffset(endSize.X,endSize.Y)
          
          ScreenPos=UDim2.fromOffset(ScreenPos.X,ScreenPos.Y)
          ScreenPosOrg =UDim2.fromOffset(ScreenPosOrg.X,ScreenPosOrg.Y)

          local image=PullFuc()
          image.Parent=UIRoot
          image.Visible=true
          local endPos=  game.Workspace.CurrentCamera:WorldToScreenPoint(WorldPos)
          endPos = UDim2.fromOffset(endPos.X,endPos.Y)
          image.Position = ScreenPosOrg
          image.Size=startSize

      local tween1 = TweenService:Create(image, ui2uiExplodeTween, {
          Size = endSize,
          Position = ScreenPos
      })

      tween1.Completed:Connect(function()
          local tween2 = TweenService:Create(image, screen2WorldTween, {
              Position = endPos
          })
          tween2.Completed:Connect(function()
              tween2:Destroy()
              PushFuc(image)
          end)
          tween2:Play()
      end)

      tween1:Play()
end;


local upTween = TweenInfo.new(0.6, Enum.EasingStyle.Back, Enum.EasingDirection.Out)
local goTagTween = TweenInfo.new(0.6, Enum.EasingStyle.Back, Enum.EasingDirection.Out)

M.RandomScreen2Tag = function(PullFuc,PushFuc,ScreenPos)
  ScreenPos=UDim2.fromOffset(ScreenPos.X,ScreenPos.Y)

    local image=PullFuc()
    image.Parent=UIRoot
    image.Visible=true
    local x=math.random(0,2,0.8)
    local y=math.random(0.3,0.6)
    image.Position=UDim2.fromScale(x,y)

    local t = TweenService:Create(image, upTween, {
      Position = UDim2.fromScale(x,y+0.1)
    })

    local t2 = TweenService:Create(image,goTagTween, {
      Position=ScreenPos
    })

    t.Completed:Connect(function()
      t2:Destroy()
      PushFuc(image)
    end)

    t.Completed:Connect(function()
      t2:Play()
    end)
    t:Play()

end;
return M